package com.example.jetpackcomposelayouts

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasurePolicy
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.unit.dp
import com.example.jetpackcomposelayouts.ui.theme.JetpackComposeBasicTheme

/**
 * @author huangzehui
 * @date 1/5/2024 下午9:45 周三
 * @description
 *
 * https://www.bilibili.com/video/BV1ob4y1a7ad?p=19&spm_id_from=pageDriver&vd_source=e07cc8cc94eb22bd76722071b03cd199
 */


@Composable
fun MyOwnColumn(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
    Layout(modifier = modifier, content = content, measurePolicy = MeasurePolicy { measurables, constraints ->
        // 对控件进行测量
        val placeables = measurables.map { measurable: Measurable ->
            measurable.measure(constraints = constraints)
        }

        var yPosition = 0

        // 布局大小
        layout(constraints.maxWidth, constraints.maxHeight) {
            placeables.forEach { placeable: Placeable ->
                // 设置元素的位置
                placeable.placeRelative(x = 0, y = yPosition)
                yPosition += placeable.height
            }
        }
    })
}


@Composable
fun MyOwnColumnSample(){
    JetpackComposeBasicTheme {
        MyOwnColumn(Modifier.padding(20.dp)) {
            Text(text = "HH")
            Text(text = "HH")
            Text(text = "HH")
            Text(text = "HH")
        }

    }
}
